<?php

class MgmtBootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('HTML5');
    }

    protected function _initAutoload()
    {
        // Custom base classes
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Backend_');
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Sozfo_');
        Zend_Loader_Autoloader::getInstance()->registerNamespace('Core_');
    }

    protected function _initSession()
    {
        $this->bootstrap('db');
        $sessionManager = new Core_Session_Manager();
        Zend_Session::setOptions(array(
            'gc_probability' => 1,
            'gc_divisor'     => 5000
        ));
        Zend_Session::setSaveHandler($sessionManager);

        $general_settings = $this->getOption('general');
        if (!empty($general_settings['sessionNamespace']))
        {
            $mgmtNamespaceName = $general_settings['sessionNamespace'];
            $mgmtNamespace     = new Zend_Session_Namespace($general_settings['sessionNamespace']);
            Zend_Registry::set('session', $mgmtNamespace);
        }
    }

    protected function _initLocale()
    {

        $this->bootstrap('db');

        $current_lang = false;

        // Try to get current language from session
        $general_settings = $this->getOption('general');
        if (!empty($general_settings['sessionNamespace']))
        {
            $mgmtNamespaceName = $general_settings['sessionNamespace'];
            $mgmtNamespace     = new Zend_Session_Namespace($general_settings['sessionNamespace']);
            if ($mgmtNamespace->lang)
            {
                $languages_list = Application_Model_Language::getList();
                foreach ($languages_list as $lang)
                {
                    if ($lang->getKey() == $mgmtNamespace->lang)
                    {
                        $current_lang = $lang;
                        break;
                    }
                }
            }
        }

        // Use default if unable to determine
        if (!$current_lang)
        {
            Application_Model_Language::determineCurrent();
            $current_lang = Application_Model_Language::getCurrent();
        }

        if ($current_lang)
        {
            Zend_Registry::set('language', $current_lang);
            require_once 'Zend/Translate.php';
            $translator = new Zend_Translate(
                'Array',
                APPLICATION_PATH . '/mgmtlocale/',
                $current_lang->getKey(),
                array(
                    'scan' => Zend_Translate::LOCALE_FILENAME
                )
            );
            Zend_Registry::set('Zend_Translate', $translator);

            $locale = new Zend_Locale();
            $locale->setLocale($current_lang->getKey());
            Zend_Registry::set('Zend_Locale', $locale);

            require_once 'Zend/Log/Writer/Stream.php';
            $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH  .'/logs/translate.mgmt.log');
            require_once 'Zend/Log.php';
            $log    = new Zend_Log($writer);
            $translator->setOptions(array(
                'log'             => $log,
                'logMessage'      => "Unknownn message '%message%' in '%locale%'",
                'logUntranslated' => true
            ));

            // Try to write value to session
            if (!empty($general_settings['sessionNamespace']))
            {
                $mgmtNamespaceName = $general_settings['sessionNamespace'];
                $mgmtNamespace     = new Zend_Session_Namespace($general_settings['sessionNamespace']);
                if ($mgmtNamespace)
                {
                    $mgmtNamespace->lang = $current_lang->getKey();
                    Zend_Registry::set('session', $mgmtNamespace);
                }
            }
        }
    }

    protected function _initDatabase()
    {
        $resource = $this->getPluginResource('db');
        $db       = $resource->getDbAdapter();
        if ($db)
        {
            Zend_Registry::set('db', $db);
        }
    }

    protected function _initNavigation()
    {

        // Initialize routing
        $this->bootstrap('FrontController');
        $front  = $this->getResource('FrontController');
        $router = $front->getRouter();
        $front->setRouter($router); 

        // Build navigation
        $this->bootstrap('view');
        $view  = $this->getResource('view');
        $pages = array(
            array(
                'label'      => 'Home',
                'title'      => 'Home',
                'controller' => 'index',
                'action'     => 'index',
                'order'      => -100,
            ),
            array(
                'label'      => 'Pages',
                'title'      => 'Pages',
                'controller' => 'page',
                'action'     => 'index',
                'order'      => 10,
            ),
            array(
                'label'      => 'Blocks',
                'title'      => 'Blocks',
                'controller' => 'blocks',
                'action'     => 'index',
                'order'      => 20,
            ),
            array(
                'label'      => 'Mail templates',
                'title'      => 'Mail templates',
                'controller' => 'mail',
                'action'     => 'index',
                'order'      => 30,
            ),
            array(
                'label'      => 'Portfolios',
                'title'      => 'Portfolios',
                'controller' => 'portfolio',
                'action'     => 'index',
                'order'      => 40,
            ),
            array(
                'label'      => 'Logout',
                'title'      => 'Logout',
                'controller' => 'index',
                'action'     => 'logout',
                'order'      => 100,
            ),
        );
 
        $container = new Zend_Navigation($pages);
        $view->getHelper('navigation')->setContainer($container);
    }

    protected function _initHelpers()
    {
        // Initialize view helpers path
        $view = $this->getResource('view');
        //$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Standart_Helper');
        $view->addHelperPath(realpath(APPLICATION_PATH . '/../library/ZendX/JQuery/View/Helper'), 'ZendX_JQuery_View_Helper');
        $view->addHelperPath(realpath(APPLICATION_PATH . '/../library/Sozfo/View/Helper'), 'Sozfo_View_Helper');
        $view->addHelperPath(realpath(APPLICATION_PATH . '/../library/Backend/Helper/View'), 'Backend_Helper_View');
        $view->addHelperPath(realpath(APPLICATION_PATH . '/../library/Backend/Helper/Widget'), 'Backend_Helper_Widget');
        Zend_Controller_Action_HelperBroker::addPrefix('Backend_Helper_Action');


    }

    protected function _initRegistry()
    {
        Zend_Registry::set('general', $this->getOption('general'));
    }

}
